package com.android.gufengmall.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.android.gufengmall.R;
import com.android.gufengmall.bean.CultureBean;
import com.android.gufengmall.bean.FolkCraftBean;

import java.util.List;

/**
 * 主页的古文化知识适配器
 */
public class FolkCraftAdapter extends RecyclerView.Adapter<FolkCraftAdapter.FolkCraftViewHolder> {

    private Context mContext;
    private List<FolkCraftBean> mFolkCraft;
    private CommentListener mListener;

    public FolkCraftAdapter(Context context, List<FolkCraftBean> folkCraft) {
        mContext = context;
        mFolkCraft = folkCraft;
    }

    @NonNull
    @Override
    public FolkCraftViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new FolkCraftViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_home_culture,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull FolkCraftViewHolder holder, int position) {
        FolkCraftBean craftBean = mFolkCraft.get(position);
        holder.mTvTitle.setText(craftBean.getTitle());
        holder.mImgLike.setImageDrawable(craftBean.isLike() ? mContext.getResources().getDrawable(R.drawable.icon_likes)
                : mContext.getResources().getDrawable(R.drawable.icon_like));
        holder.mImgCollect.setImageDrawable(craftBean.isCollect() ? mContext.getResources().getDrawable(R.drawable.icon_collects)
                : mContext.getResources().getDrawable(R.drawable.icon_collect));
        holder.mImgLike.setOnClickListener(v -> {
            mListener.onLikeListener(position);
        });
        holder.mImgCollect.setOnClickListener(v -> {
            mListener.onCollectListener(position);
        });
        holder.itemView.setOnClickListener(v -> {
            mListener.onItemSelectListener(craftBean.getTitle());
        });
    }

    @Override
    public int getItemCount() {
        return mFolkCraft.size();
    }

    public class FolkCraftViewHolder extends RecyclerView.ViewHolder {

        private TextView mTvTitle;
        private ImageView mImgLike;
        private ImageView mImgCollect;

        public FolkCraftViewHolder(@NonNull View itemView) {
            super(itemView);

            mTvTitle = itemView.findViewById(R.id.tv_title);
            mImgLike = itemView.findViewById(R.id.img_like);
            mImgCollect = itemView.findViewById(R.id.img_collect);
        }
    }

    public void setOnCommentListener(CommentListener commentListener) {
        mListener = commentListener;
    }

    public interface CommentListener{
        void onLikeListener(int position);

        void onCollectListener(int position);

        void onItemSelectListener(String position);
    }
}
